package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    private StoreMapper storeMapper;


    @Override
    public StorePO saveStore(StorePO storePO) {
        // 为 storeLogo 字段设置默认值，防止数据库报错
        if (storePO.getStoreLogo() == null) {
            storePO.setStoreLogo("");
        }
        storeMapper.insert(storePO);
        return storePO;
    }

    @Override
    public PageInfo<StorePO> pageList(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = assmbleStoreQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }

    @Override
    public void updateById(StorePO storePO) {
        storeMapper.updateById(storePO);
    }

    @Override
    public void deleteById(Long id) {
        storeMapper.deleteById(id);
        log.debug("店铺删除成功，ID: {}", id);
    }

    @Override
    public StorePO storeEdit(Integer id) {
        return storeMapper.selectById(id);
    }

    @Override
    public List<StorePO> getStoreByCity(AreaStoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        List<Long> cityIdList = query.getCityIdList();
        if(query.getLevel()!=null && cityIdList.size()>0){
            if(query.getLevel()==1){
                queryWrapper.in("province_id",cityIdList);
            }
            if(query.getLevel()==2){
                queryWrapper.in("city_id",cityIdList);
            }
            if(query.getLevel()==3){
                queryWrapper.in("area_id",cityIdList);
            }
        }
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        return pos;
    }

    @Override
    public List<ActivityVO> getActivity(Integer id) {
        return storeMapper.selectActivity(id);
    }

    /**
     * 将前端请求拼接成查询条件
     *
     * @param query
     * @return
     */
    private QueryWrapper<StorePO> assmbleStoreQuery(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();

        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }
        //门店名称
        if (StringUtils.isNotBlank(query.getStoreName())) {
            //模糊查询
            queryWrapper.like("store_name", query.getStoreName());
        }
        //负责人联系方式
        if (StringUtils.isNotBlank(query.getStoreHeadPhone())) {
            queryWrapper.like("store_head_phone", query.getStoreHeadPhone());
        }
        //审核状态
        if (query.getStoreStatus() != null) {
            queryWrapper.like("store_status", query.getStoreStatus());
        }else{
            queryWrapper.ne("store_status",2);
        }
        //省份
        if (query.getProvinceId() != null) {
            queryWrapper.like("province_id", query.getProvinceId());
        }
        //市区
        if (query.getCityId() != null) {
            queryWrapper.like("city_id", query.getCityId());
        }
        //区域
        if (query.getAreaId() != null) {
            queryWrapper.like("area_id", query.getAreaId());
        }

        //创建时间
        if (query.getEndTime() != null) {
            queryWrapper.ge("create_time", query.getStartingTime())
                    .le("create_time", query.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        queryWrapper.orderByDesc("id");

        return queryWrapper;
    }


    @Override
    public List<StorePO> storeByCityList(AreaStoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        List<Long> cityIdList = query.getCityIdList();
        queryWrapper.in("city_id", cityIdList);
        List<StorePO> storePOS = storeMapper.selectList(queryWrapper);
        System.out.println(storePOS);
        return storePOS;
    }

    @Override
    public List<StorePO> getStoreList() {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        List<StorePO> storePOS = storeMapper.selectList(queryWrapper);
        return storePOS;
    }
}